/*  SSDiamondOption.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Mon Sep 29 2003.
//

#import "SSDiamondOption.h"


@implementation SSDiamondOption

- (id)init
{
   if (self = [super init]) {
      [self setTradeoffArray:[NSMutableArray array]];
      [self setOption:[NSString string]];
      [self setUdValue1:[NSString string]];
      [self setUdValue2:[NSString string]];
      [self setUdValue3:[NSString string]];
      [self setUdValue4:[NSString string]];
      [self setUdValue5:[NSString string]];
   }
   return self;
}

- (id)initWithTradeoffArray:(NSMutableArray *)tradeoffs
                   option:(NSString *)op
                   udValue1:(NSString *)v1
                   udValue2:(NSString *)v2
                   udValue3:(NSString *)v3
                   udValue4:(NSString *)v4
                   udValue5:(NSString *)v5
{
   if (self = [super init]) {
      [self setTradeoffArray:tradeoffs];
      [self setOption:op];
      [self setUdValue1:v1];
      [self setUdValue2:v2];
      [self setUdValue3:v3];
      [self setUdValue4:v4];
      [self setUdValue5:v5];
   }
   return self;
}

- (void)setOption:(NSString *)string
{
   [string retain];
   [option release];
   option = string;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SSDiamondDataChanged"
                                                       object:nil];
   [[[NSDocumentController sharedDocumentController] currentDocument] updateChangeCount:NSChangeDone];
}

- (void)setUdValue1:(NSString *)string
{
   [string retain];
   [udValue1 release];
   udValue1 = string;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SSDiamondDataChanged"
                                                       object:nil];
   [[[NSDocumentController sharedDocumentController] currentDocument] updateChangeCount:NSChangeDone];
}

- (void)setUdValue2:(NSString *)string
{
   [string retain];
   [udValue2 release];
   udValue2 = string;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SSDiamondDataChanged"
                                                       object:nil];
   [[[NSDocumentController sharedDocumentController] currentDocument] updateChangeCount:NSChangeDone];
}

- (void)setUdValue3:(NSString *)string
{
   [string retain];
   [udValue3 release];
   udValue3 = string;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SSDiamondDataChanged"
                                                       object:nil];
   [[[NSDocumentController sharedDocumentController] currentDocument] updateChangeCount:NSChangeDone];
}

- (void)setUdValue4:(NSString *)string
{
   [string retain];
   [udValue4 release];
   udValue4 = string;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SSDiamondDataChanged"
                                                       object:nil];
   [[[NSDocumentController sharedDocumentController] currentDocument] updateChangeCount:NSChangeDone];
}

- (void)setUdValue5:(NSString *)string
{
   [string retain];
   [udValue5 release];
   udValue5 = string;

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SSDiamondDataChanged"
                                                       object:nil];
   [[[NSDocumentController sharedDocumentController] currentDocument] updateChangeCount:NSChangeDone];
}

- (NSString *)option
{
   return option;
}

- (NSString *)udValue1
{
   return udValue1;
}

- (NSString *)udValue2
{
   return udValue2;
}

- (NSString *)udValue3
{
   return udValue3;
}

- (NSString *)udValue4
{
   return udValue4;
}

- (NSString *)udValue5
{
   return udValue5;
}

- (void)setTradeoffArray:(NSMutableArray *)array
{
   [array retain];
   [tradeoffArray release];
   tradeoffArray = array;
}

- (NSMutableArray *)tradeoffArray
{
   return tradeoffArray;
}

- (void)addTradeoff:(SSDiamondTradeoff *)tradeoff
{
   [tradeoffArray addObject:tradeoff];
}

- (void)addNewTradeoff
{
   SSDiamondTradeoff *newTradeoff = [[SSDiamondTradeoff alloc] init];
   [tradeoffArray addObject:newTradeoff];
}

- (void)removeTradeoff:(SSDiamondTradeoff *)tradeoff
{
   [tradeoffArray removeObjectIdenticalTo:tradeoff];
}


//*************************
- (NSString *)xmlStringRepresentation
{
   NSString *xmlString = [NSString stringWithFormat:@"\n\t<diamond_option>\n\t\t<option>%@</option>\n\t\t<udValue1>%@</udValue1>\n\t\t<udValue2>%@</udValue2>\n\t\t<udValue3>%@</udValue3>\n\t\t<udValue4>%@</udValue4>\n\t\t<udValue5>%@</udValue5>\n\t\t<tradeoffs>\n%@\t\t</tradeoffs>\n\t</diamond_option>",
      [self option], [self udValue1], [self udValue2], [self udValue3], [self udValue4], [self udValue5], [self tradeoffsXMLString]];

      return xmlString;
}

- (NSString *)tradeoffsXMLString
{
   NSString *xmlString = [NSString string];
   NSEnumerator *enumerator = [tradeoffArray objectEnumerator];
   id anObject;

   while (anObject = [enumerator nextObject]) {
      xmlString = [xmlString stringByAppendingString:[anObject xmlStringRepresentation]];
   }
   
   return xmlString;
}

//*************************

- (void)encodeWithCoder:(NSCoder *)coder
{
   [coder encodeObject:option forKey:@"option"];
   [coder encodeObject:udValue1 forKey:@"udValue1"];
   [coder encodeObject:udValue2 forKey:@"udValue2"];
   [coder encodeObject:udValue3 forKey:@"udValue3"];
   [coder encodeObject:udValue4 forKey:@"udValue4"];
   [coder encodeObject:udValue5 forKey:@"udValue5"];
   [coder encodeObject:tradeoffArray forKey:@"tradeoffArray"];
}

- (id)initWithCoder:(NSCoder *)coder
{
   if(self = [super init]) {
      [self setOption:[coder decodeObjectForKey:@"option"]];
      [self setUdValue1:[coder decodeObjectForKey:@"udValue1"]];
      [self setUdValue2:[coder decodeObjectForKey:@"udValue2"]];
      [self setUdValue3:[coder decodeObjectForKey:@"udValue3"]];
      [self setUdValue4:[coder decodeObjectForKey:@"udValue4"]];
      [self setUdValue5:[coder decodeObjectForKey:@"udValue5"]];
      [self setTradeoffArray:[coder decodeObjectForKey:@"tradeoffArray"]];
   }
   return self;
}

//*************************

- (void)dealloc
{
   [tradeoffArray release];
   [udValue1 release];
   [udValue2 release];
   [udValue3 release];
   [udValue4 release];
   [udValue5 release];
   [option release];
   [super dealloc];
}


@end
